﻿Public Class frmAddEditComputer

  Private Sub frmAddEditComputer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Me.txtID.Text <> "" Then     'If editing a computer, fill in the selected computer information into the textboxes and combobox
      Dim strComputerInfo() As String = arlComputers.Item(Me.txtID.Text)
      Me.txtComputerName.Text = strComputerInfo(0)
      Me.cboDriveLetter.SelectedItem = strComputerInfo(1)
      Me.txtAlertSize.Text = strComputerInfo(2)
      Me.Text = Application.ProductName & ": Edit Computer"
    Else     'If adding a computer
      Me.Text = Application.ProductName & ": Add Computer"
    End If
  End Sub

  Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    ' Declare and Initialize Procedure-Level Variables and Constants
    Dim strComputerInfo(3) As String
    Dim strLocalComputerName As String = Environment.GetEnvironmentVariable("COMPUTERNAME")

    'If the computer being added/edited is the local computer, set the computer name to (local)
    If strLocalComputerName = Me.txtComputerName.Text.ToString Or Me.txtComputerName.Text.ToString.ToLower = "(local)" Then
      strComputerInfo(0) = "(local)"
    Else
      'If the computer being added/edited is not the local computer, ping the computer to see if its currently on
      Dim blnComputerOnline As Boolean = PingComputer(Me.txtComputerName.Text.ToString)

      If blnComputerOnline = True Then     'If the computer is currently on
        strComputerInfo(0) = Me.txtComputerName.Text.ToString
      Else     'If the computer is not currently on, ask if the user wishes to add it anyway, and if not, exit the sub
        If MessageBox.Show("The computer name you specified could not be contacted. Do you wish to still add this computer?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.No Then
          Exit Sub
        Else
          strComputerInfo(0) = Me.txtComputerName.Text.ToString
        End If
      End If
    End If

    'Add the rest of the information provided in the textboxes and comboboxes to the computerInfo array
    strComputerInfo(1) = Me.cboDriveLetter.SelectedItem
    strComputerInfo(2) = Me.txtAlertSize.Text
    strComputerInfo(3) = ""

    If Me.txtID.Text <> "" Then     'If editing a computer, add the information back into the Computers arraylist at its present location
      arlComputers.Item(Me.txtID.Text) = strComputerInfo
    Else     'Add the computer to the Computers arraylist
      arlComputers.Add(strComputerInfo)
    End If

    'Close the form
    Me.Close()
  End Sub

  Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
    'Close the form
    Me.Close()
  End Sub

End Class